package com.base.caselib.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.base.caselib.R;
import com.base.caselib.bean.CheckBoxBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by weikailiang on 2019/10/14.
 */

public class SingleCheckCustom extends FrameLayout{
    private Map<String,String> mMap = new HashMap<>();
    private List<CheckBoxBean> mList = new ArrayList<>();
    private RadioGroup mLayout;
    private TextView mTitle;
    private String mRowIndex;
    private CheckBoxBean mCurrentBoxBean;

    private OnCheckFuOnClick listener;
    public SingleCheckCustom(@NonNull Context context) {
        super(context);
        init(null);
    }

    public SingleCheckCustom(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public SingleCheckCustom(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }
    private void init(AttributeSet attrs){
        LayoutInflater.from(getContext()).inflate(R.layout.item_signchoice, this);
        mLayout = findViewById(R.id.rg_layout);
        mTitle = findViewById(R.id.title);

    }
    public void createChrid(List<CheckBoxBean> bean,String title){
        if (bean != null && bean.size()>0){
            if (mTitle != null){
                mTitle.setText(title);
            }
            mList.clear();
            mList.addAll(bean);
            for (final CheckBoxBean checkBoxBean : bean){
                RadioButton box = new RadioButton(getContext());

                RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                box.setLayoutParams(params);
                box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (listener != null&& mMap!= null) {
                            if (b) {
                                listener.onCheckedListener(checkBoxBean,mMap.get(checkBoxBean.getDropValue()),"");
                            } else {
                                listener.onUnCheckListener(checkBoxBean,"",mMap.get(checkBoxBean.getDropValue()));
                            }
                        }
                    }
                });
                box.setTag(checkBoxBean.getDropValue());
                box.setText(checkBoxBean.getDropLabel());
                mLayout.addView(box);
            }
        }

    }
    public void setDefault(String str){
        List<View> list = getAllChildViews(mLayout);
        for (View view : list){
            if(view instanceof RadioButton){
                RadioButton button = (RadioButton) view;
                if (TextUtils.isEmpty(str)){
                    button.setChecked(false);
                }else {
                    if (button.getText().toString().equals(str)) {
                        button.setChecked(true);
                    }
                }
            }
        }
    }


    /**
     * 根据传过来的值设置默认选中
     * @param dropLabel
     */
    public void setmCurrentBoxBean(String dropLabel,String dropValue){
        List<View> views = getAllChildViews(mLayout);
        for (View view : views) {
            if (view instanceof RadioButton) {
                RadioButton radioButton = (RadioButton) view;
                radioButton.setTag(dropValue);
               if (dropLabel.equals(radioButton.getText().toString())){
                   radioButton.setChecked(true);
               }else {
                   radioButton.setChecked(false);
               }

            }
        }
    }

    public CheckBoxBean getmCurrentBoxBean() {

        List<View> views = getAllChildViews(mLayout);

        for (View view : views){
            if (view instanceof RadioButton){
                RadioButton radioButton = (RadioButton) view;
                if (radioButton.isChecked()){
                    for (CheckBoxBean bean : mList){
                        if (radioButton.getTag().equals(bean.getDropValue())){
                            return bean;
                        }
                    }
                }
            }
        }

        return null;
    }

    /**
     * 获取单选 选中 对应的tag（dropValue）
     * @return
     */
    public String getmCurrentSingleCheckValue(){
        List<View> views = getAllChildViews(mLayout);

        for (View view : views){
            if (view instanceof RadioButton){
                RadioButton radioButton = (RadioButton) view;
                if (radioButton.isChecked()){
                    for (CheckBoxBean bean : mList){
                        if (radioButton.getTag().equals(bean.getDropValue())){
                            return String.valueOf(radioButton.getTag());
                        }
                    }
                }

            }
        }
        return "";
    }

    private List<View> getAllChildViews(View view) {
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                allchildren.add(viewchild);
                //再次 调用本身（递归）
                allchildren.addAll(getAllChildViews(viewchild));
            }
        }
        return allchildren;
    }

    public String getmRowIndex() {
        return mRowIndex;
    }

    public void setmRowIndex(String mRowIndex) {
        this.mRowIndex = mRowIndex;
    }

    public void setMapKeyValue(Map<String,String> map){
        if (map != null&&map.size()>0){
            mMap = map;
        }
    }

    public void setOnCheckListener(OnCheckFuOnClick listener){
        this.listener = listener;
    }

    public interface OnCheckFuOnClick{
        void onCheckedListener(CheckBoxBean bean, String modeIndex,String lastModeIndex);
        void onUnCheckListener(CheckBoxBean bean,String modeIndex,String lastModeIndex);
    }

}
